//
//  UnarmedListHeaderView.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/30.
//

import Foundation
import UIKit

class UnarmedListHeaderView: UIView {
    let armedItem = UnarmedListItemView()
    let unarmedItem = UnarmedListItemView()
    let delayItem = UnarmedListItemView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withModal modal: AlarmCbfdeptsModal?) {
        armedItem.val = "\(modal?.totalMap?.ybfTotal ?? 0)"
        unarmedItem.val = "\(modal?.totalMap?.unbfTotal ?? 0)"
        delayItem.val = modal?.totalMap?.ycbfTotal ?? "0"
    }
    
    func setupUI() {
        let baseV = UIView()
        baseV.backgroundColor = .bg
        baseV.layer.cornerRadius = 10
        baseV.layer.masksToBounds = true
        addSubview(baseV)
        baseV.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(10)
            make.left.equalTo(self.snp.left).offset(10)
            make.right.equalTo(self.snp.right).offset(-10)
            make.bottom.equalTo(self.snp.bottom).offset(-10)
        }
        
//        let itemWidth = (ScreenWidth - 20.0) / 3.0
        let itemWidth = (ScreenWidth - 20.0) / 2.0
        armedItem.key = "已布防"
        armedItem.valL.textColor = .hex("#0F499E")
        baseV.addSubview(armedItem)
        armedItem.snp.makeConstraints { make in
            make.top.equalTo(baseV.snp.top)
            make.left.equalTo(baseV.snp.left)
            make.width.equalTo(itemWidth)
        }
        
        let vLine1 = UIView()
        vLine1.backgroundColor = .hex("#E8E8E8")
        addSubview(vLine1)
        vLine1.snp.makeConstraints { make in
            make.left.equalTo(armedItem.snp.right)
            make.centerY.equalTo(self.snp.centerY)
            make.height.equalTo(42)
            make.width.equalTo(0.5)
        }

        unarmedItem.key = "未布防"
        unarmedItem.valL.textColor = .hex("#FF0000")
        baseV.addSubview(unarmedItem)
        unarmedItem.snp.makeConstraints { make in
            make.centerY.equalTo(armedItem.snp.centerY)
            make.left.equalTo(armedItem.snp.right)
            make.width.equalTo(itemWidth)
        }
        
//        let vLine2 = UIView()
//        vLine2.backgroundColor = .hex("#E8E8E8")
//        addSubview(vLine2)
//        vLine2.snp.makeConstraints { make in
//            make.left.equalTo(unarmedItem.snp.right)
//            make.centerY.equalTo(self.snp.centerY)
//            make.height.equalTo(42)
//            make.width.equalTo(0.5)
//        }
//
//        delayItem.key = "延迟布防"
//        delayItem.valL.textColor = .hex("#1E6C00")
//        baseV.addSubview(delayItem)
//        delayItem.snp.makeConstraints { make in
//            make.centerY.equalTo(armedItem.snp.centerY)
//            make.left.equalTo(unarmedItem.snp.right)
//            make.width.equalTo(itemWidth)
//        }
    }
}

class UnarmedListItemView: UIView {

    var key: String = "" {
        didSet {
            keyL.text = key
        }
    }

    var val: String = "" {
        didSet {
            valL.text = val
        }
    }

    let keyL = UILabel()
    let valL = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI() {
        keyL.font = .systemFont(ofSize: 14)
        keyL.textColor = .hex("#666666")
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(18)
            make.centerX.equalToSuperview()
        }
        
        valL.text = "0"
        valL.font = .systemFont(ofSize: 17)
        valL.textAlignment = .center
        addSubview(valL)
        valL.snp.makeConstraints { make in
            make.top.equalTo(keyL.snp.bottom).offset(6)
            make.centerX.equalToSuperview()
        }
    }
}
